<template>
    <div>
      <operation-title
        title="轮播搜索"
        :icon="TitleIcon"
      />
      <div class="divider"></div>
      <div class="func">
        <p class="attr">边角样式</p>
        <ul class="common-ul">
          <li @click="config.pageConfig.custom = '1'" :class="config.pageConfig.custom == '1' ? 'active' : ''">
            <img src="../../../../../../assets/app/other/banner/arc.png" alt="" />
          </li>
          <li @click="config.pageConfig.custom = '2'" :class="config.pageConfig.custom == '2' ? 'active' : ''">
            <img src="../../../../../../assets/app/other/banner/round.png" alt="" />
          </li>
          <li @click="config.pageConfig.custom = '3'" :class="config.pageConfig.custom == '3' ? 'active' : ''">
            <img src="../../../../../../assets/app/other/banner/square.png" alt="" />
          </li>
        </ul>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">颜色选择</p>
        <div class="adjust-item">
          <span>图标</span>
          <el-color-picker v-model="config.pageConfig.iconcolor" />
          <span>{{ config.pageConfig.iconcolor }}</span>
        </div>
        <div class="adjust-item">
          <span>文字</span>
          <el-color-picker v-model="config.pageConfig.textcolor" />
          <span>{{ config.pageConfig.textcolor }}</span>
        </div>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">透明度</p>
        <div class="adjust-item">
          <el-slider :min="30" :max="100" v-model="config.pageConfig.opacity" show-input size="small" />%
        </div>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">轮播点位置</p>
        <ul class="common-ul">
          <li @click="config.pageConfig.position = '1'" :class="config.pageConfig.position == '1' ? 'active' : ''">
            <img src="../../../../../../assets/app/other/banner/left.png" alt="" />
          </li>
          <li @click="config.pageConfig.position = '2'" :class="config.pageConfig.position == '2' ? 'active' : ''">
            <img src="../../../../../../assets/app/other/banner/center.png" alt="" />
          </li>
          <li @click="config.pageConfig.position = '3'" :class="config.pageConfig.position == '3' ? 'active' : ''">
            <img src="../../../../../../assets/app/other/banner/right.png" alt="" />
          </li>
        </ul>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">轮播图片</p>
        <h4>建议图片宽度750px，高度368px。</h4>
        <ul v-if="
          config.pageConfig &&
          config.pageConfig.imgList &&
          config.pageConfig.imgList.length > 0
        " class="add-item">
          <li v-for="(item, index) in config.pageConfig.imgList" :key="index">
            <div class="upload-img" v-if="item.imgUrl">
              <el-image style="width: 100%; height: 100%" :src="item.imgUrl" fit="contain"></el-image>
              <div class="upload-img_cover">
                <Upload :model-value="fileList" file-path="act/" :limit="999" :show-file-list="false"
                  @success="uploadSuccess">
                  <el-button @click="currentIndex = index" size="small">替换</el-button>
                </Upload>
              </div>
            </div>
            <Upload v-else v-model="item.imgUrl" file-path="act/" list-type="picture-card">
              <el-icon>
                <Plus />
              </el-icon>
            </Upload>
            <div class="link-div" @click="linkDialog(item,index)">
              <el-icon>
                <Link />
              </el-icon>
              <span v-if="item.url">{{item.urlName}}</span>
              <span v-else>请选择链接</span>
              <!-- <el-input v-model="item.url" placeholder="请填写带有https/http的链接地址" clearable></el-input> -->
              <!-- <span v-if="item.name">请选择链接</span>
              <span v-else>请选择链接</span> -->
              <!-- <el-icon v-if="item.name" class="close-btn"><Close /></el-icon> -->
            </div>
            <div class="move-btns">
            <el-icon
              v-show="index != 0"
              class="move-icon"
              title="上移"
              @click="moveTo('up', index)"
            >
              <Top />
            </el-icon>
            <el-icon
              v-show="index != config.pageConfig.imgList.length - 1"
              style="cursor: pointer"
              title="下移"
              @click="moveTo('down', index)"
            >
              <Bottom />
            </el-icon>
            </div>
            <el-popconfirm confirm-button-text="是" cancel-button-text="否" @confirm="sliceItem(index)" :title="
              config.pageConfig.imgList.length > 1
                ? '确定删除吗？'
                : '只有一个不可删除'
            ">
              <template #reference>
                <el-icon class="sure-close">
                  <Close />
                </el-icon>
              </template>
            </el-popconfirm>
          </li>
          <li class="extra-li" @click="addItem" v-if="config.pageConfig.imgList.length < 8">
            <p>+添加{{ config.pageConfig.imgList.length }}/8</p>
          </li>
        </ul>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">样式</p>
        <ul class="common-ul">
          <li @click="config.pageConfig.style = '1'" :class="config.pageConfig.style == '1' ? 'active' : ''">
            默认
          </li>
          <li @click="config.pageConfig.style = '2'" :class="config.pageConfig.style == '2' ? 'active' : ''">
            投影
          </li>
          <li @click="config.pageConfig.style = '3'" :class="config.pageConfig.style == '3' ? 'active' : ''">
            描边
          </li>
        </ul>
        <div class="adjust-item" v-if="config.pageConfig.style == '3'">
          <el-color-picker v-model="config.pageConfig.borderColor" />
          <span>{{ config.pageConfig.borderColor }}</span>
        </div>
        <div class="adjust-item" v-if="config.pageConfig.style == '3'">
          <el-slider v-model="config.pageConfig.borderWidth" :min="0" :max="5" show-input size="small" />px
        </div>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">颜色选择</p>
        <div class="adjust-item">
          <span>背景色</span>
          <el-color-picker v-model="config.pageConfig.color" />
          <span>{{ config.pageConfig.color }}</span>
        </div>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">边距</p>
        <div class="adjust-item">
          <span>上边距</span>
          <el-slider v-model="config.pageConfig.marginTop" :min="0" :max="50" show-input size="small" />px
        </div>
        <div class="adjust-item">
          <span>下边距</span>
          <el-slider v-model="config.pageConfig.marginBottom" :min="0" :max="50" show-input size="small" />px
        </div>
        <div class="adjust-item">
          <span>左右边距</span>
          <el-slider v-model="config.pageConfig.margin" :min="0" :max="20" show-input size="small" />px
        </div>
      </div>
      <div class="divider"></div>
      <div class="func">
        <p class="attr">圆角设置</p>
        <div class="adjust-item">
          <span>上圆角</span>
          <el-slider :min="0" :max="20" v-model="config.pageConfig.radiusTop" show-input size="small" />px
        </div>
        <div class="adjust-item">
          <span>下圆角</span>
          <el-slider :min="0" :max="20" v-model="config.pageConfig.radiusBottom" show-input size="small" />px
        </div>
      </div>
      <div class="divider"></div>

      <href-selector
          v-model:show="linkFormVisible"
          :model-value="linkObject"
          @confirm="(link) => submitUrl(link, config.pageConfig.imgList[linkIndex])"/>
    </div>
  </template>

  <script lang="ts">
  export default {
    name: "BannerSearchOperation",
  };
  </script>
  <script lang="ts" setup>
  import { reactive, onMounted } from "vue";
  import {useHrefSelector} from "@/views/activity/actApp/components/default/compositions/useHrefSelector";
  import TitleIcon from "@/assets/app/icon/bannerSearch.png";
  import OperationTitle
    from "@/views/activity/actApp/components/default/operationComponents/operationTitle/OperationTitle.vue";


  const config = defineProps({
    pageConfig: {
      type: Object,
      default: {
        custom: "1",
        iconcolor:"#fd463e",
        textcolor:'#5e6066',
        color: "",
        searchWord:'',
        opacity:80,
        position:'2',
        style:'1',
        borderColor: "#ededed",
        borderWidth: 1,
        marginTop: 0,
        marginBottom: 0,
        marginImg: 5,
        margin: 0,
        radiusTop: 0,
        radiusBottom: 0,
        imgList: [
          {
            imgUrl: "",
            name: "",
            url: "",
            urlType: '',
            urlName: '',
          },
          {
            imgUrl: "",
            name: "",
            url: "",
            urlType: '',
            urlName: '',
          },
        ],
      },
    },
  });
  const sliceItem = (idx: Number) => {
    if (config.pageConfig.imgList.length < 2) {
      return;
    } else {
      config.pageConfig.imgList.splice(idx, 1);
    }
  };
  const currentIndex = ref(999);
  const fileList = ref([]);
  const addItem = () => {
    if (config.pageConfig.imgList.length >= 8) {
      return;
    } else {
      config.pageConfig.imgList.push({
        imgUrl: "",
        name: "",
        url: "",
      });
    }
  };
  let emits = defineEmits<{ (e: "changeStyle", configStyle: Object): void }>();

  const uploadSuccess = (value: any) => {
    config.pageConfig.imgList[currentIndex.value].imgUrl = value.url;
  };
//   let linkIndex = 0;
// const linkFormVisible = ref(false);
// const linkObject = ref({
//   linkUrl: '',
//   linkName: '',
//   linkType: '',
//   linkHttpUrl: ''
// });
// const submitUrl = (content: any) => {
//   config.pageConfig.imgList[linkIndex].url = content.url;
//   config.pageConfig.imgList[linkIndex].urlName = content.urlName;
//   config.pageConfig.imgList[linkIndex].urlType = content.urlType;
//   linkFormVisible.value = false
// };
// const onClose = () => {
//   linkFormVisible.value = false
// };
// const linkDialog = (item: any, idx: number) => {
//   linkIndex = idx;
//   if (item.urlType == 'link') {
//     linkObject.value.linkHttpUrl = item.url;
//     linkObject.value.linkUrl = '';
//     linkObject.value.linkName = item.urlName;
//     linkObject.value.linkType = item.urlType;
//   } else {
//     linkObject.value.linkHttpUrl = '';
//     linkObject.value.linkUrl = item.url;
//     linkObject.value.linkName = item.urlName;
//     linkObject.value.linkType = item.urlType;
//   }
//   linkFormVisible.value = true;
// };

const moveTo=(type:'up'|'down',index:number)=>{
  const list = [...config.pageConfig.imgList]
  const item = list[index]
  //上移
  if (type == 'up') {
    const preItem = list[index - 1]
    list[index - 1] = item
    list[index] = preItem
  }
  //下移
  if (type == 'down') {
    const nextItem = list[index + 1]
    list[index] = nextItem
    list[index + 1] = item
  }
  config.pageConfig.imgList = [...list]
}

const {linkFormVisible, linkIndex, linkObject, submitUrl, linkDialog} = useHrefSelector();


  watch(config, (val) => {
    emits("changeStyle", config.pageConfig);
  });
  </script>

  <style lang="scss" scoped>
  .title {
    padding: 8px 23px;
    overflow: hidden;
    border-bottom: 5px solid #f6f7f9;
    display: flex;
    position: relative;
    height: 50px;
    box-sizing: border-box;

    img {
      position: absolute;
      left: 0;
      top: -5px;
    }

    h5 {
      font-size: 16px;
      font-weight: 700;
      position: absolute;
      left: 70px;
      top: 0;
      line-height: 50px;
    }
  }

  .func {
    border-bottom: 5px solid #f6f7f9;
    padding: 20px 16px;
    font-size: 16px;

    .attr {
      line-height: 1;
      color: #47565d;
      font-weight: bolder;
      margin-bottom: 20px;
    }

    ul.common-ul {
      list-style: none;
      display: flex;
      align-items: center;
      justify-content: center;

      li {
        flex: 1;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 4px 0 0 4px;
        border: 2px solid #ededed;
        border-collapse: collapse;

        &.active {
          border: 2px solid #fb6638;
        }

        img {
          max-width: 100% !important;
          max-height: 100% !important;
        }
      }
    }

    h4 {
      position: relative;
      padding: 8px 16px 8px 16px;
      border-radius: 6px;
      color: red;
      font-size: 16px;
      line-height: 22px;
      margin-bottom: 10px;
      border: 1px solid #d8e8fc;
      background-color: #e5f0fe;
    }

    div.adjust-item {
      display: flex;
      align-items: center;
      margin-top: 3px;
      margin-bottom: 10px;

      span {
        width: 64px;
        text-align: right;
        margin-right: 15px;
        color: #666;
      }

      :deep(.el-slider) {
        width: 240px;
        margin-right: 3px;
        margin-left: 15px;
      }

      :deep(.el-slider__runway.show-input) {
        margin-right: 15px;
      }

      :deep(.el-input-number) {
        width: 100px;
        background: #f4f3f7;
        border: 1px solid #f4f3f7;
      }
    }

    .add-item {
      width: 100%;

      li {
        background: #f4f3f7;
        border-radius: 4px;
        padding: 22px 20px 10px 10px;
        color: #999;
        margin-top: 10px;
        margin-bottom: 20px;
        position: relative;
        display: flex;
        &.extra-li {
          justify-content: center;
        }

        .upload-img {
          width: 50px;
          height: 50px;
          line-height: 50px;
          margin-right: 10px;
          border: 1px dashed #ededed;
          text-align: center;
          color: #dad9d9;
          background-size: 100% 100%;
          background-repeat: no-repeat;
          float: left;
          position: relative;

          :deep(.el-image) {
            width: 50px;
            height: 50px;
          }

          &:hover {
            .upload-img_cover {
              opacity: 1;
            }
          }

          .upload-img_cover {
            opacity: 0;
            position: absolute;
            transition: all 0.3s ease;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-evenly;
          }
        }

        :deep(.el-upload-list) {
          width: 50px;
          height: 50px;
          margin-right: 10px;
        }

        :deep(.el-upload--picture-card) {
          width: 50px;
          height: 50px;
        }

        .link-div {
          width: calc(100% - 80px);
          display: flex;
          align-items: center;
          background: #e8effc;
          border: 0;
          padding-left: 16px;
          cursor: pointer;
          position: relative;

          :deep(.el-icon) {
            margin-right: 10px;
          }

          :deep(.el-input) {
            font-size: 12px;
            height: 30px;
            width: calc(100% - 30px);
          }

          .close-btn {
            position: absolute;
            right: 5px;
          }
        }

        .sure-close {
          position: absolute;
          right: 5px;
          top: 5px;
          cursor: pointer;
        }
      }
    }
  }
.move-btns {
  position: absolute;
  top: 5px;
  right: 24px;
  .move-icon {
    margin-right: 4px;
    cursor: pointer;
  }
}

  :deep(.el-radio__label) {
    font-size: 16px;
  }
  </style>
